//
//  GameEndLayer.m
//  Maze1
//
//  Created by student on 11/8/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "GameEndLayer.h"
#define CONTINUE 0
#define QUITBUTTON 1

@implementation GameEndLayer
@synthesize room = _room;

-(id)initWithRoom:(Room*)aRoom
{
	if ((self = [super init])) {
        
        self.room = aRoom;
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        [self setupMenu];
        //set background of layer;
        CCSprite* bg = [CCSprite spriteWithFile:@"GameOverLayer.png"];
        bg.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:bg z:-1];
       
        
        //winner
        CCSprite* builder = [CCSprite spriteWithFile:@"Icon.png"];
        builder.position = ccp(winSize.width/2 - builder.contentSize.width*2, winSize.height*0.4);
        [self addChild:builder z:1];
        
        //add other player

        for (Player* player in self.room.otherPlayersList){
            CCSprite* player = [CCSprite spriteWithFile:@"Icon.png"];
            player.position = ccp(winSize.width/2 + player.contentSize.width*2, winSize.height*0.4);
            [self addChild:player z:1];
        }
        
	}
	return self;
}

-(void)dealloc
{
    [_room release];
    _room = nil;
    
    
	[super dealloc];
}

-(void)setupMenu
{
	CCMenuItemImage * menuItem1 = [CCMenuItemImage itemFromNormalImage:@"ContinueButton.png"
														 selectedImage: @"ContinueButton.png"
																target:self
															  selector:@selector(transit:)];
	menuItem1.tag = CONTINUE;
	CCMenuItemImage * menuItem2 = [CCMenuItemImage itemFromNormalImage:@"QuitButton.png"
														 selectedImage: @"QuitButton.png"
																target:self
															  selector:@selector(transit:)];
	
	menuItem2.tag = QUITBUTTON;
    
    // Create a menu and add your menu items to it
	CCMenu *myMenu = [CCMenu menuWithItems:menuItem1, menuItem2, nil];
	
	myMenu.position = ccp(500, 100);
	// Arrange the menu items vertically
	[myMenu alignItemsHorizontally];
	
	// add the menu to your scene
	[self addChild:myMenu z:1];
}

-(void)transit:(CCMenuItem*)sender
{
	//connect to server
	PackageParser *aParser = [PackageParser sharedPackageParser];
	if(sender.tag == CONTINUE){
        //change player state
        aParser.state = PlayerStateStartingGame;
        //transit into RoomInfo Layer;
        
    }
    else if(sender.tag == QUITBUTTON){
        
        aParser.state = PlayerStateLoggingOut;
        //transit to RoomList Layer
    }
    	//else show connection error page
    aParser = nil;
}
@end
